//密钥定期更新
	@Component
	public static class KeyManagementTask {

		@Autowired
		private AttachmentService attachmentService;

		@Autowired
		private UserService userService;

		@Scheduled(fixedRate = 259200000) // 每3天执行一次，单位为毫秒
		public void updateKeys() {
			try {
				// 获取所有已加密的文件
				List<Attachment> attachments = attachmentService.findAllEncryptedAttachments();

				for (Attachment attachment : attachments) {
					// 获取旧密钥
					User user = userService.findbyId(attachment.getArticle().getCopywriter().getUserid());
					String oldKey = new String(attachment.getEncodekey(user), StandardCharsets.UTF_8);
					String filePath = Constants.PROJECT_ROOT_DIRECTORY + attachment.getFileid();
					System.out.println("旧密钥："+ oldKey);

					// 使用旧密钥解密文件
					byte[] encryptedData = Files.readAllBytes(Paths.get(filePath));
					byte[] decryptedData = SM4Tools.decode(encryptedData, oldKey.getBytes(StandardCharsets.UTF_8));

					// 生成新密钥
					String newKey = SM4Tools.generateRandomString(16);

					// 使用新密钥重新加密文件
					byte[] reEncryptedData = SM4Tools.encode(decryptedData, newKey.getBytes(StandardCharsets.UTF_8));
					Files.write(Paths.get(filePath), reEncryptedData);

					// 更新数据库中的密钥信息
					attachment.setEncodekey(newKey, user);
					attachmentService.updateAttachment(attachment);

					// 记录密钥更新日志
					logKeyUpdate(attachment.getAttachmentid(), oldKey, newKey);
				}

			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		private void logKeyUpdate(Integer attachmentId, String oldKey, String newKey) {
			// 实现密钥更新日志记录
			// 例如，将日志写入文件
			try (BufferedWriter writer = new BufferedWriter(new FileWriter("key_update_log.txt", true))) {
				writer.write("Attachment ID: " + attachmentId + ", Old Key: " + oldKey + ", New Key: " + newKey + ", Update Time: " + new Date());
				writer.newLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}